﻿using System;
using System.Data;
using System.Windows.Forms;
using CXMLStudioIDE.Common.Entities;
using CXMLStudioIDE.DataAccessLayer;

namespace CXMLStudioIDE.Forms
{
    public partial class AddExistingEntityForm : Form
    {
        #region Declarations

        private readonly MainForm frm_parent;
        private readonly CXMLStudioDAL m_dal;
        private readonly Project m_project;
        private readonly int m_selected_project;

        #endregion

        #region Constructor

        public AddExistingEntityForm(Form parent, int i_TreeViewID)
        {
            InitializeComponent();
            frm_parent = parent as MainForm;
            pnlAddEntity.Visible = true;
            m_dal = new CXMLStudioDAL();
            dsContext = m_dal.GetContext();
            cmbContext.DisplayMember = dsContext.Tables[0].Columns[1].ColumnName;
            cmbContext.ValueMember = dsContext.Tables[0].Columns[0].ColumnName;
            cmbContext.DataSource = dsContext.Tables[0];
            m_selected_project = i_TreeViewID;
            m_project = frm_parent.m_SolutionManager.m_solution.Projects.GetProjectByTreeviewID(m_selected_project);
        }

        #endregion

        #region Method Implementations

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void cmbContext_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbBusinessEntity.DataSource = null;
            cmbBusinessEntity.Items.Clear();
            if ((int) cmbContext.SelectedValue > 0)
            {
                var dal = new CXMLStudioDAL();
                dsBusinessEntity = dal.GetBusinessEntitiesInContext((int) cmbContext.SelectedValue);
                cmbBusinessEntity.DisplayMember = dsBusinessEntity.Tables[0].Columns[1].ColumnName;
                cmbBusinessEntity.ValueMember = dsBusinessEntity.Tables[0].Columns[0].ColumnName;
                cmbBusinessEntity.DataSource = dsBusinessEntity.Tables[0];
            }
        }

        private void lbTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (lbTemplates.Items.Count == 0)
            {
                MessageBox.Show(this, "Please assign Business entities", "CXMLStudioIDE", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
            }
            else
            {
                Cursor = Cursors.WaitCursor;
                if (frm_parent.m_SolutionManager.m_solution != null &&
                    frm_parent.m_SolutionManager.m_solution.Projects != null)
                {
                    foreach (BusinessEntity b in lbTemplates.Items)
                    {
                        frm_parent.m_SolutionManager.AddEntityToProject(m_project.ID, b);
                    }
                }
                frm_parent.UpdateMainForm();
                Cursor = Cursors.Default;
                Dispose();
            }
        }

        private void lblTemplates_Click(object sender, EventArgs e)
        {
        }

        private void cmbBusinessEntity_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbVersion.DataSource = null;
            cmbVersion.Items.Clear();
            if ((int) cmbBusinessEntity.SelectedValue > 0)
            {
                var dal = new CXMLStudioDAL();
                dsVersion = dal.GetBusinessEntityInContextVersion((int) cmbBusinessEntity.SelectedValue);
                cmbVersion.DisplayMember = dsVersion.Tables[0].Columns[0].ColumnName;
                cmbVersion.ValueMember = dsVersion.Tables[0].Columns[1].ColumnName;
                cmbVersion.DataSource = dsVersion.Tables[0];
            }
        }

        private void btnAssign_Click(object sender, EventArgs e)
        {
            var be = new BusinessEntity();
            be.ParentProject = m_project;
            be.Name = ((DataRowView) cmbBusinessEntity.SelectedItem).Row.ItemArray[1].ToString();
            be.EntityType.EntityTypeID = (int) cmbBusinessEntity.SelectedValue;
            be.EntityVersion.EntityVersionID = (int) cmbVersion.SelectedValue;
            be.EntityTypeVersions.EntityTypeVersionsID =
                Convert.ToInt32(((DataRowView) cmbVersion.SelectedItem).Row.ItemArray[2].ToString());
            be.EntityVersion.FullVersion = ((DataRowView) cmbVersion.SelectedItem).Row.ItemArray[0].ToString();
            be.EntityType.EntityTypeContextID = (int) ((DataRowView) cmbVersion.SelectedItem).Row["EntityTypeContextID"];
            be.EntityType.EntityTypeContextName =
                ((DataRowView) cmbVersion.SelectedItem).Row["EntityTypeContextName"].ToString();
            if (m_project.Entities.Contains(be.EntityType.EntityTypeContextID, be.EntityVersion.FullVersion, be.Name))
            {
                MessageBox.Show(this, "Entity already in Project. Select a different one", "CXMLStudioIDE",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                lbTemplates.Items.Add(be);
                lbTemplates.Sorted = true;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lbTemplates.SelectedIndex >= 0)
            {
                lbTemplates.Items.RemoveAt(lbTemplates.SelectedIndex);
            }
            else
            {
                MessageBox.Show(this, "Select an item to remove", "CXMLStudioIDE", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
            }
        }

        #endregion
    }
}